package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-04-11
 * Time: 15:56
 */
class Singleton2 {
    //1.不是立即就初始化实例
    private static volatile Singleton2 instance = null;
    //2.把构造方法设为private
    private Singleton2() {}
    //3.提供一个方法获取到上述单例的实例
    //  只有当真正使用这个实例的时候,才会创建这个实例
    public static Singleton2 getInstance() {
        if(instance == null) {
            synchronized (Singleton2.class) {
                if(instance == null) {
                    instance = new Singleton2();
                }
            }
        }

        return instance;
    }
}
public class Demo11 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
    }

}
